require(['modules/fetch'],function(fetch){
    
    function processBarCtrl(){
        var $mainContent = $(".main-content");
        var contentHeight = $mainContent.height();
        var contentWidth = $mainContent.width();
        var clientHeight = $(window).height();
        var $processBar = $(".main-processbar");
        $processBar.css({
            width: contentWidth,
        });
        var processBarLen = $processBar.find(".processbar-len");
        $(window).on("scroll", function(){
            var scrollTop = $(window).scrollTop();
            var process =Math.ceil((scrollTop + clientHeight) / $(".main-content").height() * 100);
            console.log(scrollTop,clientHeight, $(".main-content").height() ,process)
            if(process > 100){
                process = 100;
            }
            processBarLen.stop();
            processBarLen.animate({
                width: process + "%",
            },800);
            $processBar.find(".processbar-desc").css({
                marginLeft: process + "%",
            }).html(process+ "%");
        });
       
    }

    var loadStoryChapterListFn = function (storyId){
        fetch('listChaptersByStoryId', { ps: 20, pn: 0, story_id: storyId},
            function (result) {
                if(0 == result.errno ){
                    vueMainData.sidebarDataList = result.data.list;
                }
            },
            function(data) {
                console.log("ajax faild");
            }
        );
    }

    
    
    function getChapterInfo (chapterId){
        fetch('getChapterInfo', { chapter_id: chapterId},
            function (result) {
                if(0 == result.errno ){
                    $(window).scrollTop(0);
                    vueMainData.chapterInfo = result.data.chapter_info
                    vueMainData.htmlContent = markdown.toHTML(vueMainData.chapterInfo.content);
                    if(result.data.parent_node_id){
                        vueMainData.parent_node_id  = result.data.parent_node_id;
                    } else {
                        vueMainData.parent_node_id = null;
                    }
                    processBarCtrl();
                    
                }
            },
            function(data) {
                console.log("ajax faild");
            }
        );
    }

    getChapterInfo(PageData.chapterInfo.chapter_id);

    function readNodeChapter (storyId, nodeId){
        fetch('getNodeChaptersInfo', { node_id: nodeId},
            function (result) {
                if(0 == result.errno ){
                    var data = result.data;
                    var chapterInfo = data[0];
                    if(chapterInfo && chapterInfo.chapter_id){
                        getChapterInfo(parseInt(chapterInfo.chapter_id));
                        // location.href="/chapter/readChapter?chapter_id=" + parseInt(chapterInfo.chapter_id) ;
                    } else {
                        
                        if(PageData.userInfo.user_id &&PageData.userInfo.user_id == vueMainData.chapterInfo.author_id ){
                            var go = confirm("要继续创作吗？");
                            if(go){
                                location.href="/chapter/editChapter?story_id=" + parseInt(storyId) + "&node_id=" + parseInt(nodeId);
                            }
                        } else {
                            alert("此章节还没有完成");
                        }
                  
                    }
                    return false;
                }
            },
            function(data) {
                console.log("ajax faild");
            }
        );
    }

    //main
    var vueMainData = {
        chapterInfo: {},
        authorInfo: PageData.authorInfo,
        sidebarDataList:[],
        htmlContent:"",
        parent_node_id:null,
    };
    vueMainData.chapterInfo = PageData.chapterInfo;
    new Vue({
        el: "#vue-app",
        data: vueMainData,
        methods:{
            rewriteBtnClick: function(storyId, chapterId, nodeId){
                location.href="/chapter/editChapter?story_id=" + parseInt(storyId) + "&node_id=" + parseInt(nodeId);
                return false;
            },
            nodeBtnClick: function(index,item){
                console.log(arguments);
                readNodeChapter(item.story_id, item.node_id);
                return false;
            },
            prevChapterBtnClick: function(storyId, nodeId){
                console.log(arguments);
                readNodeChapter(storyId, nodeId);
            },
            sidebarItemClick: function(item, index) {
                getChapterInfo(parseInt(item.chapter_id));
                //location.href="/chapter/readChapter?chapter_id=" + parseInt(item.chapter_id); 
            }
        }
    });
    loadStoryChapterListFn(PageData.chapterInfo.story_id);

    

});